package 力扣._00周赛._430周赛.问题2;

import java.util.Random;

class Solution2 {
    public String answerString(String word, int numFriends) {
        if(numFriends==1)
            return word;
        int l = word.length();
        int maxLength = l - numFriends + 1; // 每段最大允许的长度
        String maxStr = "";

        // 贪心法：从左到右选择字典序最大的子串
        for (int i = 0; i < l; i++) {
            String subStr = word.substring(i, Math.min(i + maxLength, l));
            if (subStr.compareTo(maxStr) > 0) {
                maxStr = subStr;
            }
        }
        return maxStr;
    }



    public static void main(String[] args) {
        Solution2 s = new Solution2();
        // 随机生成一个字符串，长度为5000
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5000; i++) {
            sb.append((char) (random.nextInt(26) + 'a'));
        }
        String word = sb.toString();
        int numFriends = 2;
        System.out.println(s.answerString(word, numFriends));
    }
}